package com.headfirst.observer.version2;

import com.headfirst.observer.version1.DisplayElement;
import com.headfirst.observer.version1.Subject;

import java.util.Observable;
import java.util.Observer;

/**
 * @author sunhongguang
 * @create 2022-01-19-0:04
 */
public class ForecastDisplay_v2 implements Observer, DisplayElement {
    private float currentPressure = 29.92f;
    private float lastPressure;
    private Observable observable;

    public ForecastDisplay_v2(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {
        System.out.print("Forecast: ");
        if (currentPressure > lastPressure) {
            System.out.println("Improving weather on the way!");
        } else if (currentPressure == lastPressure) {
            System.out.println("More of the same");
        } else if (currentPressure < lastPressure) {
            System.out.println("Watch out for cooler,rainy weather");
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData_v2) {
            WeatherData_v2 weatherDataV2 = (WeatherData_v2) o;
            lastPressure = currentPressure;
            currentPressure = weatherDataV2.getPressure();
            display();
        }
    }
}
